﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace AgLux
{
    /// <summary>
    /// //
    /// </summary>

    public class Animation : Component
    {
        
      //  protected Canvas _screen;
        protected TextBlock _text;
        Storyboard _animation;
        
        double _height;
        bool _autoStart;
        /// <summary>
        /// //
        /// </summary>

        /// <param name="source">
        /// //
        /// </param>

        /// <param name="filling">
        /// //
        /// </param>

        /// <param name="caption">
        /// //
        /// </param>

        public Animation(string source, double height, bool autostart)
            : base()
        {
             _height = height;
             _screen = (Canvas)_resource.FindName("screen");
             _text = (TextBlock)_resource.FindName("text");
             
             Downloader downloader = new Downloader();

            downloader.Completed += new EventHandler(downloader_Completed);
            downloader.Open("GET", new Uri(source, UriKind.Relative));

            downloader.Send();
           // Storyboard1 = (Storyboard)_resource.FindName("Storyboard1");
           // test = (Rectangle)_resource.FindName("rectangle");

            _screen.Visibility = Visibility.Visible;

            _autoStart = autostart;
            
        }

         void downloader_Completed(object sender, EventArgs e)
        {
            //InitializeFromXaml();
            //containerCanvas.Children.Clear();
            Downloader d = (Downloader)sender;
            Canvas child = (Canvas)XamlReader.Load(d.ResponseText);
            _animation = (Storyboard)child.FindName("animation");

            
            
           // _text.Text = d.ResponseText.ToString();
            
            _screen.Children.Add(child);
            //_screen.Children.Add(_startAnimation);

            //_screen.MouseLeftButtonUp += new MouseEventHandler(_startAnimationMouseLeftButtonUp);

            if (!_autoStart)
            {
                _screen.MouseLeftButtonUp += new MouseEventHandler(_startAnimationMouseLeftButtonUp);
            }
         
            

        }

         void _startAnimationMouseLeftButtonUp(object sender, MouseEventArgs e)
         {
             
                 _animation.Begin();
                         
         }

         public override void FitCanvas(double canvasWidth)
         {
             base.FitCanvas(canvasWidth);
             _screen.Width = Width = canvasWidth;
             _screen.Height = Height = _height;
            
     

         }


}


   
}
